require_relative "../Expr"

# Expression for an expression like 10(2)
#
# @author Paul T.
class Calcula::Exprs::ApplyExpr < Calcula::Expr

  # @param base [Calcula::Expr]
  # @param params [Array<Calcula::Expr>]
  def initialize(base, params)
    @base = base
    @params = params
  end

  # @see Calcula::Expr#to_s
  # @param (see Calcula::Expr#to_s)
  # @return (see Calcula::Expr#to_s)
  def to_s(form: :src)
    baseTxt = @base.to_s(form: form)
    paramsTxt = @params.map { |e| e.to_s(form: form) }.join(",")
    case form
    when :src then
      "#{baseTxt}(#{paramsTxt})"
    when :ruby then
      "#{baseTxt}.(#{paramsTxt})"
    when :tree then
      "(#{baseTxt} #{paramsTxt})"
    else
      nil
    end
  end

  # @see Calcula::Expr#children
  # @param (see Calcula::Expr#children)
  # @return (see Calcula::Expr#children)
  def children
    [@base, *@params]
  end
end
